package com.ibeeking.found.report.rest.controller.v1;

import com.ibeeking.found.report.rest.service.IBaseService;
import com.ibeeking.found.report.rest.service.IReportDictItemService;
import com.ibeeking.found.report.rest.service.IReportDictService;
import com.ibeeking.found.report.service.common.bo.KeyValue;
import com.ibeeking.found.report.service.common.dos.ReportDictDO;
import com.ibeeking.found.report.service.common.po.ResponseBean;
import com.ibeeking.found.report.service.common.query.ReportDictPageQuery;
import com.ibeeking.found.report.service.common.vo.ReportDictVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * (ReportDictDO)实体类
 *
 * @author lr
 * @since 2021-02-23 10:01:02
 */
@RestController
@RequestMapping("/dict/v1")
public class DmpReportDictController extends DmpBaseController<ReportDictPageQuery, ReportDictDO, ReportDictVO> {

    @Resource
    private IReportDictService reportDictServiceImpl;

    @Resource
    private IReportDictItemService reportDictItemServiceImpl;

    @Override
    public IBaseService<ReportDictPageQuery, ReportDictDO> getService() {
        return reportDictServiceImpl;
    }

    @Override
    public ReportDictDO getEntity() {
        return new ReportDictDO();
    }

    @Override
    public ReportDictVO getDTO() {
        return new ReportDictVO();
    }


    /**
     * 刷新指定字典项
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "刷新指定字典项")
    @PostMapping("/freshDict")
    public ResponseBean refreshDict(@RequestBody List<String> dictCodes) {
        //刷新
        reportDictServiceImpl.refreshCache(dictCodes);
        return responseSuccess();
    }

    /**
     * 下拉菜单
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询字典项")
    @GetMapping("/select/{dictCode}")
    public ResponseBean select(@PathVariable("dictCode") String dictName){
        Locale locale = LocaleContextHolder.getLocale();
        //语言
        String language = locale.getLanguage();

        List<KeyValue> keyValues = reportDictServiceImpl.select(dictName,language);
        return responseSuccessWithData(keyValues);
    }


    /**
     * 指定语言的字典项
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "指定语言的字典项")
    @GetMapping("/map/{dictCode}")
    public ResponseBean dictItemByLang(@PathVariable("dictCode") String dictCode){
        return responseSuccessWithData(reportDictItemServiceImpl.getItemMap(dictCode));
    }
    /**
     * 下拉菜单
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询字典项")
    @GetMapping("/selectAll/{project}")
    public ResponseBean selectTypecodes(@PathVariable String project){
        Locale locale = LocaleContextHolder.getLocale();
        //语言
        String language = locale.getLanguage();

        Collection<KeyValue> keyValues = reportDictServiceImpl.selectTypeCode(project,language);
        return responseSuccessWithData(keyValues);
    }

    /**
     * 获取所有字典
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取所有字典")
    @GetMapping("/all")
    public ResponseBean all(){
        Locale locale = LocaleContextHolder.getLocale();
        //语言
        String language = locale.getLanguage();

        Map<String, List<KeyValue>> all = reportDictServiceImpl.all(language);
        return responseSuccessWithData(all);
    }
}
